package web.fabrimind.tas.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import web.fabrimind.common.annotation.Log;
import web.fabrimind.common.core.controller.BaseController;
import web.fabrimind.common.core.domain.AjaxResult;
import web.fabrimind.common.enums.BusinessType;
import web.fabrimind.pm.domain.ProductionPlan;
import web.fabrimind.tas.domain.MaterialInboundApply;
import web.fabrimind.tas.service.IMaterialInboundApplyService;
import web.fabrimind.common.utils.poi.ExcelUtil;
import web.fabrimind.common.core.page.TableDataInfo;
/**
 * 物料入库申请Controller
 * 
 * @author ruoyi
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/tas/MaterialInboundApply")
public class MaterialInboundApplyController extends BaseController
{
    @Autowired
    private IMaterialInboundApplyService materialInboundApplyService;

    /**
     * 查询物料入库申请列表
     */
    @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialInboundApply materialInboundApply)
    {
        startPage();
        List<MaterialInboundApply> list = materialInboundApplyService.selectMaterialInboundApplyList(materialInboundApply);
        return getDataTable(list);
    }

    /**
     * 导出物料入库申请列表
     */
    @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:export')")
    @Log(title = "物料入库申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialInboundApply materialInboundApply)
    {
        List<MaterialInboundApply> list = materialInboundApplyService.selectMaterialInboundApplyList(materialInboundApply);
        ExcelUtil<MaterialInboundApply> util = new ExcelUtil<MaterialInboundApply>(MaterialInboundApply.class);
        util.exportExcel(response, list, "物料入库申请数据");
    }

    /**
     * 获取物料入库申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:query')")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") Long applyId)
    {
        return success(materialInboundApplyService.selectMaterialInboundApplyByApplyId(applyId));
    }

    /**
     * 新增物料入库申请
     */
    @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:add')")
    @Log(title = "物料入库申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialInboundApply materialInboundApply)
    {
        return toAjax(materialInboundApplyService.insertMaterialInboundApply(materialInboundApply));
    }

    /**
     * 修改物料入库申请
     */
    @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:edit')")
    @Log(title = "物料入库申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialInboundApply materialInboundApply)
    {
        return toAjax(materialInboundApplyService.updateMaterialInboundApply(materialInboundApply));
    }

    /**
     * 删除物料入库申请
     */
    @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:remove')")
    @Log(title = "物料入库申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable Long[] applyIds)
    {
        return toAjax(materialInboundApplyService.deleteMaterialInboundApplyByApplyIds(applyIds));
    }


    /**
     * 查询用户(申请人和审核人)列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('tas:user:list')")
    @GetMapping("/user/getlist")
    public AjaxResult getUserList(MaterialInboundApply materialInboundApply) {
        List<MaterialInboundApply> userList = materialInboundApplyService.selectUserList(materialInboundApply);
        return AjaxResult.success(userList);
    }


    /**
     * 查询供应商列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('tas:supplier:list')")
    @GetMapping("/supplier/getlist")
    public AjaxResult getSupplierList(MaterialInboundApply materialInboundApply) {
        List<MaterialInboundApply> supplierList = materialInboundApplyService.selectSupplierList(materialInboundApply);
        return AjaxResult.success(supplierList);
    }


    // @PreAuthorize("@ss.hasPermi('tas:MaterialInboundApply:edit')")
    // @PutMapping("/submit/{applyId}")
    // public AjaxResult submit(@PathVariable("applyId") Long applyId)
    // {
    //     // 验证申请是否存在
    //     MaterialInboundApply apply = materialInboundApplyService.selectMaterialInboundApplyByApplyId(applyId);
    //     if (apply == null) {
    //         return AjaxResult.error("申请不存在");
    //     }
        
    //     // 验证状态是否为草稿状态(4)
    //     if (!"4".equals(apply.getStatus())) {
    //         return AjaxResult.error("只有草稿状态的申请才能提交");
    //     }
        
    //     // 更新状态为已提交(1)
    //     apply.setStatus("1");
    //     apply.setApplyTime(DateUtils.getNowDate());
        
    //     // 获取当前登录用户
    //     LoginUser loginUser = getLoginUser();
    //     apply.setApplyUserId(loginUser.getUserId());
    //     apply.setUpdateBy(loginUser.getUsername());
        
    //     return toAjax(materialInboundApplyService.updateMaterialInboundApply(apply));
    // }





}
